<?php
class Home extends CI_Controller
{
	var $data = array(
		'categories'	=> array(),
		'title'			=> '',
		'contacts'		=> array(),
	);
	function __construct()
	{
		parent::__construct();
		$this->lang->load('common');
		$this->load->helper('date');
		$this->load->model('Category','',TRUE);
		$this->load->model('Topic','',TRUE);
		$this->load->model('Contact','',TRUE);
		$this->load->model('Config','',TRUE);
		
		//Load contacts
		$temp_query = $this->Contact->get_contacts();
		$this->data['contacts'] = $temp_query->result_array();
		
		
		//Load Menu
		$temp_query=$this->Category->get_sub_categories(0);
		$categories=$temp_query->result_array();
		$left_menu = array();
		$li=0;
		$cat_count = count($categories);
		for($i=0;$i<$cat_count;$i++)
		{
			$category=$categories[$i];
			if($category['leftmenu']==1){
				$left_menu[$li++]=$category;
				unset($categories[$i]);
				continue;
			}
			//Load sub Menu
			$temp_query=$this->Category->get_sub_categories($category['id']);
			if($temp_query->num_rows>0)
				$categories[$i]['sub_categories']=$temp_query->result_array();
		}
		
		$leftmenu_max = $this->Config->val('max_topic_leftmenu');
		$datestring = '%d/%m/%Y';
		for($i=0;$i<count($left_menu);$i++)
		{
			$temp_query=$this->Topic->get_topics($left_menu[$i]['id'],3);
			$left_menu[$i]['topics'] = $temp_query->result_array();
			for($j=0;$j<count($left_menu[$i]['topics']);$j++)
			{
				$left_menu[$i]['topics'][$j]['date'] = mdate($datestring,$left_menu[$i]['topics'][$j]['update_date']);
			}
		}
		
		$this->data['categories']= array_merge($this->data['categories'],$categories);
		
		$this->data['left_menu'] = $left_menu;
		
		
		
		
		$this->data = array_merge($this->data,array(
			'copyright'		=> $this->Config->val('copyright'),
			'contact_info'	=> $this->Config->val('contact_info'),
			'title'			=> $this->Config->val('site_name'),
		));
	}
	function index()
	{
		$id = 0;
		$temp_query=$this->Topic->get_topics($id);
		$datestring="%d/%m%Y %h:%i %a";
		if($temp_query->num_rows==1)
		{
			$topics = $temp_query->result_array();
			$id=$topics[0]['id'];
			$datestring=$this->Config->val('datestring');
			$topic=$this->Topic->get_content($id);
			if($topic)
			{
				$temp = $this->Category->get_category_info($topic['cid']);
				$topic['catname']=$temp['name'];
				$topic['last_update'] = mdate($datestring,$topic['update_date']);
				
				
				//Commit with $this->data
				$this->data['topic'] = $topic;
			}			
		}
		else
		{
			$this->data['category']	= array(
				'id'	=> 0,
				'name'	=> $this->lang->line('HOME'),
			);
			$topics = $temp_query->result_array();
			for($i=0;$i<count($topics);$i++)
			{
			$topics[$i]['last_update'] = mdate($datestring,$topics[$i]['update_date']);
			}
			$this->data['topics'] = $topics;
		}
		$this->load->model('Slide','',TRUE);
		$this->data['slide_image'] = $this->Slide->get_all();
		$this->load->view('header',$this->data);
		$this->load->view('slide');
		$this->load->view('content');
		$this->load->view('footer');
	}
	function category($id)
	{
		$temp_query=$this->Topic->get_topics($id);
		$datestring=$this->Config->val('datestring');
		if($temp_query->num_rows==1)
		{
			$topics = $temp_query->result_array();
			redirect('home/topic/'.$topics[0]['id']);
		}
		else
		{
			$temp=$this->Category->get_category_info($id);
			$this->data['category']	= $temp;
			$topics = $temp_query->result_array();
			for($i=0;$i<count($topics);$i++)
			{
				$topics[$i]['last_update'] = mdate($datestring,$topics[$i]['update_date']);
			}
			$this->data['topics'] = $topics;
		}
		
		$this->load->view('header',$this->data);
		$this->load->view('content');
		$this->load->view('footer');
	}
	function topic($id)
	{
		$datestring=$this->Config->val('datestring');
		$topic=$this->Topic->get_content($id);
		if($topic)
		{
			$temp = $this->Category->get_category_info($topic['cid']);
			$topic['catname']=$temp['name'];
			$topic['last_update'] = mdate($datestring,$topic['update_date']);
			
			
			//Commit with $this->data
			$this->data['topic'] = $topic;
		}
		$this->load->view('header',$this->data);
		$this->load->view('content');
		$this->load->view('footer');
	}
}
?>